const ApiLog = require('../models/ApiLog.js');

const getApiLogs = async (ctx) => {
  try {
    const page = parseInt(ctx.query.page) || 1;
    const pageSize = parseInt(ctx.query.pageSize) || 10;
    const offset = (page - 1) * pageSize;

    const { count, rows } = await ApiLog.findAndCountAll({
      offset: offset,
      limit: pageSize,
      order: [['createdAt', 'DESC']], // Show newest logs first
    });

    ctx.body = {
      code: 200,
      data: {
        total: count,
        records: rows,
      },
      message: 'Success',
    };
  } catch (error) {
    ctx.status = 500;
    ctx.body = { code: 500, message: error.message };
  }
};

module.exports = {
  getApiLogs,
};